<?php
use yii\helpers\Html;
$post=\Yii::$app->session->getFlash('post');
?>
<?php $this->beginBlock('title'); ?>
    <?php if(isset($model['game_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏
<?php $this->endBlock();?>
<?php $this->beginBlock('breadcrumb'); ?>
<li><a href="<?php echo \Yii::$app->urlManager->createUrl('/game/index')?>">游戏</a></li>
<li class="active"><a href="<?php echo \Yii::$app->urlManager->createUrl('/game/edit',['game_id'=>isset($model['game_id'])?$model['game_id']:0]);?>"><?php if(isset($model['game_id'])):?>编辑<?php else:?>新增<?php endif;?>游戏</a></li>
<?php $this->endBlock();?>

<link rel="stylesheet" type="text/css" href="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/css/fileinput.css" />
<style>
.show_upload { display:none; }
</style>
<div class="row">
	<div class="col-sm-12">
		<section class="panel">
			<div class="panel-body">
				<div class="form">
					<form class="cmxform form-horizontal tasi-form" method="post" action="<?php echo \Yii::$app->urlManager->createUrl('/game/save');?>">
						<div class="form-group">
							<label for="game_name" class="control-label col-lg-2">名称</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="game_name" name="game_name" value="<?php if(isset($post['game_name'])){ echo $post['game_name'];}else{ echo Html::encode(isset($model['game_name'])?$model['game_name']:''); }?>" type="text" required />
                            </div>
						</div>
						<div class="form-group">
                            <label for="short_introduce" class="control-label col-lg-2">游戏介绍</label>
                            <div class="col-lg-10">
                                <textarea class="form-control " id="short_introduce" name="short_introduce" required><?php if(isset($post['short_introduce'])){ echo $post['short_introduce'];}else{ echo Html::encode(isset($model['short_introduce'])?$model['short_introduce']:''); }?></textarea>
                            </div>
                        </div>
						<div class="form-group">
                            <label for="path" class="control-label col-lg-2">所属状态</label>
                            <div class="col-lg-10">
                                <select class="form-control" style="width:30%;display:inline;" name="state_id">
									<?php foreach($states as $item):?>
					                	<option value="<?=$item['state_id'];?>"<?php if(isset($post['state_id']) && $post['state_id']==$item['state_id']):?> selected<?php elseif (isset($model['state_id']) && $model['state_id']==$item['state_id']):?> selected<?php endif;?>><?=$item['state_name'];?></option>
					                <?php endforeach;?>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                        	<label for="type" class="control-label col-lg-2">所属类型：</label>
                        	<div class="col-lg-10">
	                            <div id="linkSelecter" style="margin-bottom: 10px;display: none;">
	                                <select class="form-control sub_type" style="width:25%;display:inline;" tabindex="1" name="type[]">
	                                    <option value="0">--请选择类型--</option>
	                                    <?php foreach($types as $item):?>
	                                        <option value="<?=$item['type_id'];?>"><?=$item['type_name'];?></option>
	                                    <?php endforeach;?>
	                                </select>
	                                <button type="button" class="btn btn-primary addSelecter">增加</button>
	                                <button type="button" class="btn btn-danger removeSelecter">删除</button>
	                            </div>
	                            <div id="link_list">
	                                <?php if($this_game_types):?>
	                                    <?php foreach($this_game_types as $key1=>$item1):?>
	                                        <div id="linkSelecter" style="margin-bottom: 10px;">
	                                           <?php foreach($item1 as $key2=>$item2):?>
	                                           <select class="form-control sub_type" tabindex="1" style="width:25%;display:inline;" name="type[]">
                                                    <?php foreach($item2 as $key3=>$item3):?>
            	                                        <option value="<?=$item3['type_id'];?>"<?php if($item3['type_id']==$this_game_types_id[$key1][$key2]):?> selected<?php endif;?>><?=$item3['type_name'];?></option>
            	                                    <?php endforeach;?>
	                                            </select>
	                                            <?php endforeach;?>
	                                            <button type="button" class="btn btn-primary addSelecter">增加</button>
	                                            <button type="button" class="btn btn-danger removeSelecter">删除</button>
	                                        </div>
	                                    <?php endforeach;?>
	                                <?php else:?>
	                                    <div style="margin-bottom: 10px;">
	                                        <select class="form-control sub_type" tabindex="1" style="width:25%;display:inline;" name="type[]">
	                                            <option value="0">--请选择--</option>
	                                            <?php foreach($types as $item):?>
        	                                        <option value="<?=$item['type_id'];?>"><?=$item['type_name'];?></option>
        	                                    <?php endforeach;?>
	                                        </select>
	                                        <button type="button" class="btn btn-primary addSelecter">增加</button>
	                                        <button type="button" class="btn btn-danger removeSelecter">删除</button>
	                                    </div>
	                                <?php endif;?>
	                            </div>
                        	</div>
                    	</div>
                        <div class="form-group">
                            <label for="pinyin" class="control-label col-lg-2">全写拼音</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="pinyin" type="text" value="<?php if(isset($post['pinyin'])){ echo $post['pinyin'];}else{ echo Html::encode(isset($model['pinyin'])?$model['pinyin']:''); }?>" name="pinyin" required />
                            </div>
                        </div>
                        <?php if(isset($model['game_id'])):?>
                        <div class="form-group">
                            <label for="game_downclick" class="control-label col-lg-2">下载数量</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="game_downclick" type="text" disabled="" value="<?php if(isset($post['game_downclick'])){ echo $post['game_downclick'];}else{ echo Html::encode(isset($model['game_downclick'])?$model['game_downclick']:''); }?>" name="game_downclick" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="game_visits" class="control-label col-lg-2">访问数量</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="game_visits" type="text" disabled="" value="<?php if(isset($post['game_visits'])){ echo $post['game_visits'];}else{ echo Html::encode(isset($model['game_visits'])?$model['game_visits']:''); }?>" name="game_visits" />
                            </div>
                        </div>
                        <?php endif;?>
                        <div class="form-group">
                            <label for="score" class="control-label col-lg-2">分数值</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="score" type="text" name="score" value="<?php if(isset($post['score'])){ echo $post['score'];}else{ echo Html::encode(isset($model['score'])?$model['score']:50); }?>" required />
                                <span class="help-block">满分为100，默认是50分</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="sort_num" class="control-label col-lg-2">排序值</label>
                            <div class="col-lg-10">
                                <input class="form-control " id="sort_num" type="text" name="sort_num" value="<?php if(isset($post['sort_num'])){ echo $post['sort_num'];}else{ echo Html::encode(isset($model['sort_num'])?$model['sort_num']:0); }?>" required />
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="number" class="control-label col-lg-2">Logo图片</label>
                            <div class="col-lg-10">
                            	<?php if(isset($model['game_logo'])):?>
	                            	<span class="file-input">
	                            		<div class="file-preview">
	                            			<div class="close fileinput-remove">×</div>
	                            			<div class="">
	                            				<div class="file-preview-thumbnails">
													<div data-fileindex="0" class="file-preview-frame">
	   													<img style="width:auto;height:160px;" alt="<?php echo isset($model['game_name'])?$model['game_name']:'';?>" title="<?php echo isset($model['game_name'])?$model['game_name']:'';?>" class="file-preview-image" src="<?=filepath($model['game_logo']);?>">
	   													<div class="file-thumbnail-footer">
	    													<div style="width: 170px;" class="file-caption-name" title="<?php echo isset($model['game_name'])?$model['game_name']:'';?>"><?php echo isset($model['game_name'])?$model['game_name']:'';?></div>
														</div>
													</div>
												</div>
												<div class="clearfix"></div>
											</div>
										</div>
									</span>
									<div class="show_upload">
										<input id="file_game_logo" class="file" type="file" name="file" multiple=true>
			                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
									</div>
                            	<?php else:?>
                            		<input id="file_game_logo" class="file" type="file" name="file" multiple=true>
		                        	<span class="help-block">请上传[jpg、png、gif]格式的图片，建议规格为500*500，不大于1M </span>
		                        <?php endif;?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="isdel" class="control-label col-lg-2">是否删除</label>
                            <div class="col-lg-10">
                                <div class="radio-custom radio-success">
		                            <input type="radio" value="1" <?php if(isset($post['isdel']) && $post['isdel']==1):?>checked<?php elseif(isset($model['isdel']) && $model['isdel']==1):?>checked<?php endif;?> name="isdel" id="delyes">
		                            <label for="delyes">是</label>
		                            <input type="radio" value="0" <?php if(isset($post['isdel']) && $post['isdel']==0):?>checked<?php elseif(isset($model['isdel']) && $model['isdel']==0):?>checked<?php endif;?> name="isdel" id=delno>
		                            <label for="delno">否</label>
		                        </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="is_comment" class="control-label col-lg-2">是否推荐</label>
                            <div class="col-lg-10">
                                <div class="radio-custom radio-success">
		                            <input type="radio" value="1" <?php if(isset($post['is_comment']) && $post['is_comment']==1):?>checked<?php elseif(isset($model['is_comment']) && $model['is_comment']==1):?>checked<?php endif;?> name="is_comment" id="commentyes">
		                            <label for="commentyes">推荐</label>
		                            <input type="radio" value="0" <?php if(isset($post['is_comment']) && $post['is_comment']==0):?>checked<?php elseif(isset($model['is_comment']) && $model['is_comment']==0):?>checked<?php endif;?> name="is_comment" id="commentno">
		                            <label for="commentno">不推荐</label>
		                        </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="search_comment" class="control-label col-lg-2">搜索中推荐</label>
                            <div class="col-lg-10">
                                <div class="radio-custom radio-success">
		                            <input type="radio" value="1" <?php if(isset($post['search_comment']) && $post['search_comment']==1):?>checked<?php elseif(isset($model['search_comment']) && $model['search_comment']==1):?>checked<?php endif;?> name="search_comment" id="searchyes">
		                            <label for="searchyes">推荐</label>
		                            <input type="radio" value="0" <?php if(isset($post['search_comment']) && $post['search_comment']==0):?>checked<?php elseif(isset($model['search_comment']) && $model['search_comment']==0):?>checked<?php endif;?> name="search_comment" id="searchno">
		                            <label for="searchno">不推荐</label>
		                        </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="is_new" class="control-label col-lg-2">是否新游</label>
                            <div class="col-lg-10">
                                <div class="radio-custom radio-success">
		                            <input type="radio" value="1" <?php if(isset($post['is_new']) && $post['is_new']==1):?>checked<?php elseif(isset($model['is_new']) && $model['is_new']==1):?>checked<?php endif;?> name="is_new" id="is_newyes">
		                            <label for="is_newyes">是</label>
		                            <input type="radio" value="0" <?php if(isset($post['is_new']) && $post['is_new']==0):?>checked<?php elseif(isset($model['is_new']) && $model['is_new']==0):?>checked<?php endif;?> name="is_new" id="is_newno">
		                            <label for="is_newno">否</label>
		                        </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="is_hot" class="control-label col-lg-2">是否热门</label>
                            <div class="col-lg-10">
                                <div class="radio-custom radio-success">
		                            <input type="radio" value="1" <?php if(isset($post['is_hot']) && $post['is_hot']==1):?>checked<?php elseif(isset($model['is_hot']) && $model['is_hot']==1):?>checked<?php endif;?> name="is_hot" id="is_hotyes">
		                            <label for="is_hotyes">是</label>
		                            <input type="radio" value="0" <?php if(isset($post['is_hot']) && $post['is_hot']==0):?>checked<?php elseif(isset($model['is_hot']) && $model['is_hot']==0):?>checked<?php endif;?> name="is_hot" id="is_hotno">
		                            <label for="is_hotno">否</label>
		                        </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="qqun" class="control-label col-lg-2">QQ群文字</label>
                            <div class="col-lg-10">
                                <textarea class="form-control " id="qqun" name="qqun"><?php if(isset($post['qqun'])){ echo $post['qqun'];}else{ echo Html::encode(isset($model['qqun'])?$model['qqun']:''); }?></textarea>
                            </div>
                        </div>
                        <div class="form-group">
							<label for="qqunlink" class="control-label col-lg-2">QQ群链接</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="qqunlink" name="qqunlink" value="<?php if(isset($post['qqunlink'])){ echo $post['qqunlink'];}else{ echo Html::encode(isset($model['qqunlink'])?$model['qqunlink']:''); }?>" type="text" />
                            </div>
						</div>
                        <div class="form-group">
                            <label for="huodong" class="control-label col-lg-2">活动文字</label>
                            <div class="col-lg-10">
                                <textarea class="form-control " id="huodong" name="huodong"><?php if(isset($post['huodong'])){ echo $post['huodong'];}else{ echo Html::encode(isset($model['huodong'])?$model['huodong']:''); }?></textarea>
                            </div>
                        </div>
                        <div class="form-group">
							<label for="huodonglink" class="control-label col-lg-2">活动链接</label>
							<div class="col-lg-10">
                               	<input class="form-control" id="huodonglink" name="huodonglink" value="<?php if(isset($post['huodonglink'])){ echo $post['huodonglink'];}else{ echo Html::encode(isset($model['huodonglink'])?$model['huodonglink']:''); }?>" type="text" />
                            </div>
						</div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-10">
                                <input type="hidden" name="_csrf" id="_csrf" value="<?=\Yii::$app->request->csrfToken; ?>" />
        						<?php if(isset($model['game_id'])):?><input type="hidden" name="game_id" value="<?=$model['game_id'];?>" /><?php endif;?>
        						<input type="hidden" value="<?php echo isset($model['game_logo'])?$model['game_logo']:'';?>" id="game_logo" name="game_logo" />
                                <button class="btn btn-success" type="submit"> 保存</button>
                                <button class="btn btn-default" type="button" onclick="javascript:history.go(-1)"> 取消</button>
                            </div>
                        </div>
                    </form>
                </div>
			</div>
		</section>
	</div>
</div>

<?php
$this->beginBlock('script');
?>
<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl; ?>/js/bootstrap-fileinput-master/js/fileinput.js"></script>
<script type="text/javascript">
//约束上传图片条件
$("#file_game_logo").fileinput({
    allowedFileExtensions : ['jpg', 'png','gif'],
    showPreview : true,
    maxFileSize: 1000,
    minImageWidth: 500,
    minImageHeight: 500,
    showUpload:false,
    //maxFilesNum: 1,
});

$(function() {
	//开始上传图片
    $(document).on('change', "#file_game_logo", function() {
    	var csrfToken = $('meta[name="csrf-token"]').attr("content");
        $.ajaxFileUpload({
            url : "<?php echo \Yii::$app->urlManager->createUrl('/storage/upload')?>",
            secureuri : false,
            fileElementId : 'file_game_logo',
            dataType : 'json',
            data : { _csrf:csrfToken },
            success : function (data, status) {
                $("#game_logo").val(data.path);
            },
            error : function (data, status, e) {
                alert(e);
            }
        });
    });
});

//修改图片时，关闭图片显示改为上传
$(".fileinput-remove").click(function(){
	$(".file-input").toggle();
	$("#game_logo").val("");
	$(".show_upload").show();
	$(".file-input-new").show();
});

//产生下级游戏类型
$(document).on('change', ".sub_type", function() {
    var obj=$(this);
    var parent_id = $(this).val();
    $(this).nextAll(':not(button)').remove();
    $.getJSON("<?=\Yii::$app->urlManager->createUrl('game-type/sub-types');?>", { parent_id:parent_id }, function(data) {
        if (data.length > 0) {
            var select = '&nbsp;&nbsp;<select class="form-control sub_type" style="width:25%;display:inline;" name="type[]" tabindex="1"><option value="0">--请选择--</option>';
            $(data).each(function(i, e) {
                select += "<option value='"+ e.type_id+"'>"+ e.type_name+"</option> ";
            });
            select += "</select>";
            obj.after(select);
        }
    });
});

//增加一行
$(document).on('click', '.addSelecter', function(){
    addLinkSelecter();
});

//删除一行
$(document).on('click', '.removeSelecter', function()
{
    var parent = $(this).parent();
    var del = false;
    if (parent.find('[name="cust[]"]').val() != '') {
        if (confirm('确定要删除吗？')) {
            del = true;
        }
    } else {
        del = true;
    }
    if (del) {
        $(this).parent().remove();
        $("#link_list .addSelecter:last").show();
        if ($("#link_list .removeSelecter").size() <= 1 ) {
            $("#link_list .removeSelecter").hide();
        }
    }
});

//克隆函数
function addLinkSelecter()
{
    $("#link_list .addSelecter").hide();
    var link = $("#linkSelecter").clone().show();
    $("#link_list").append(link);
    $("#link_list .removeSelecter").show();
}

$("#link_list .addSelecter:not(':last')").hide();
</script>
<?php $this->endBlock('script');?>